package org.sith.taskrunner.gwt.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.Editor;
import com.google.gwt.editor.client.SimpleBeanEditorDriver;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.*;
import org.sith.taskrunner.gwt.client.resources.TaskRunnerMessages;
import org.sith.taskrunner.gwt.shared.proxy.RoleProxy;
import org.sith.taskrunner.gwt.shared.proxy.UserEntityProxy;


public class UserManageWidget extends Composite implements Editor<UserEntityProxy>, IUserManageView {

    private Driver driver;

    private Presenter presenter;

    private UserEntityProxy originalUser;

    public void setPresenter(Presenter presenter) {
        this.presenter = presenter;
    }

    interface UserManageWidgetUiBinder extends UiBinder<HTMLPanel, UserManageWidget> {
    }

    interface Driver extends SimpleBeanEditorDriver<UserEntityProxy, UserManageWidget> {
    }

    private static UserManageWidgetUiBinder ourUiBinder = GWT.create(UserManageWidgetUiBinder.class);
    @Path("active")
    @UiField
    CheckBox activeField;
    @Path("role")
    @UiField
    EnumEditor<RoleProxy> roleField;
    @Path("password")
    @UiField
    TextBox passwordField;
    @Path("login")
    @UiField
    TextBox loginField;
    @UiField
    Button saveButton;
    @UiField
    Button cancelButton;
    @UiField
    Button removeButton;
    @UiField
    @Ignore
    Label resultStatusLabel;
    @UiField
    HorizontalPanel statusPanel;


    public UserManageWidget() {

        initWidget(ourUiBinder.createAndBindUi(this));
        driver = GWT.create(Driver.class);


        driver.initialize(this);

    }


    @Override
    public void setResultLabel(boolean isSuccess) {
        statusPanel.setVisible(isSuccess);
        if (isSuccess) {
            resultStatusLabel.setText(TaskRunnerMessages.App.getInstance().success());
            resultStatusLabel.addStyleName("greenLabel");
        } else {
            resultStatusLabel.setText(TaskRunnerMessages.App.getInstance().failed());
            resultStatusLabel.addStyleName("redLabel");
        }
    }

    @Override
    public void restoreView() {
        setEditedObject(originalUser);
    }

    @Override
    public Widget getViewWidget() {
        return this;
    }

    @Override
    public void setEditedObject(UserEntityProxy userEntityProxy) {
        originalUser = userEntityProxy;
        driver.edit(userEntityProxy);

        loginField.setEnabled(false);
        passwordField.setEnabled(false);
        removeButton.setVisible(true);
    }

    @Override
    public UserEntityProxy getEditedObject() {
        return driver.flush();
    }

    @UiHandler("saveButton")
    public void handleSaveButtonClick(ClickEvent event) {
        presenter.onSaveButtonClicked();
    }

    @UiHandler("removeButton")
    public void handleRemoveButtonClick(ClickEvent event) {
        presenter.onRemoveButtonClicked();
    }

    @UiHandler("cancelButton")
    public void handlCancelButtoneClick(ClickEvent event) {
        presenter.onCancelButtonClicked();
    }
}